<?php

class CounterCacheBehavior extends ModelBehavior {

    public function setup(&$Model, $settings) {
        
    }

    public function count(&$model) {
        $varname = "CounterCacheBehavior.count." . $model->alias;
        $count = Cache::read($varname);
        if ($count == null) {
            $count = $model->find('count');
            Cache::write($varname, $count);
        }
    }

    public function afterSave(&$model, $created) {
        parent::afterSave($model, $created);
        $this->changed($model);
    }

    public function afterDelete(&$model) {
        parent::afterDelete($model);
        $this->changed($model);
    }

    private function changed(&$model) {
        $varname = "CounterCacheBehavior.count." . $model->alias;
        $count = $model->find('count');
        Cache::write($varname, $count);
    }
}

?>
